﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Ad
{
    public partial class Ad : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员是否登录
            this.checkAdminIsLogin();

            if (!Page.IsPostBack)
            {
                //绑定下拉框
                this.bindTypeID();
                this.bindAdClassID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        AdController.getController().doDelegeAd(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定广告类型
        /// </summary>
        public void bindTypeID()
        {
            dropTypeID.Items.Insert(0, new ListItem("--请选择--", ""));
            dropTypeID.Items.Insert(1, new ListItem("文字", "0"));
            dropTypeID.Items.Insert(2, new ListItem("图片", "1"));
        }

        /// <summary>
        /// 绑定广告分类
        /// </summary>
        public void bindAdClassID()
        {
            dropAdClassID.DataSource = AdController.getController().getAdClassList(100);
            dropAdClassID.DataValueField = "ID";
            dropAdClassID.DataTextField = "AdClassName";
            dropAdClassID.DataBind();
            dropAdClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }
        /// <summary>
        /// 绑定修改数据
        /// </summary>
        private void bindmodel()
        { 
             int ID =int.Parse(this.GetQuery("ID"));
             hf.Model.Ad model = AdController.getController().getAdModel(ID);

             dropTypeID.SelectedValue = model.TypeID.ToString();
             dropAdClassID.SelectedValue = model.AdClassID.ToString();
             tbTitle.Text = model.Title;
             tbPicUrl.Text = model.PicUrl;
             tbHttpUrl.Text = model.HttpUrl;

             IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             string Action = this.GetQuery("action");
             if (Action == "modify")
             {
                 AdController.getController().doModifyAd(int.Parse(IDS.Value),
                     dropTypeID.SelectedValue,
                     dropAdClassID.SelectedValue,
                     tbTitle.Text,
                     tbPicUrl.Text,
                     tbHttpUrl.Text);
             }
             else
             {
                 AdController.getController().doAddAd(dropTypeID.SelectedValue,
                     dropAdClassID.SelectedValue,
                     tbTitle.Text,
                     tbPicUrl.Text,
                     tbHttpUrl.Text);
             }
        }
    }
}